package shellstation;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.ResultSet;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;


public class Userview extends JFrame implements ActionListener {

    private JFrame frame;
    int u;
    private JPanel panel;
    private JPanel panel2;
    private JLabel UserType;
    private JLabel Username;
    private JComboBox Usertype;
    private String[] listUser = {"Administrator", "Cashier"};
    private JTextField Uname;
    private DefaultTableModel listOfUsers;
    private DefaultTableModel listOfP;
    private String[] username;
    private String[] columnName = {"UserType", "Username"};

    private JTable table;
    private JTable productList;

    private JScrollPane scrollpane;
    private JScrollPane scrollpane2;
    private Icon searchIcon;
    private Icon editIcon;
    private Icon deleteIcon;
    private JButton search;
    private JButton delete;
    private JButton edit;
    private JScrollPane scrollPanel;
    private int size;

    private JTable UserTable;
 
    public void Userview(){
        UserController uc = new UserController();

     editIcon = new ImageIcon(getClass().getResource("image/editUser.gif"));
     deleteIcon = new ImageIcon(getClass().getResource("image/deleteUser.gif"));
     searchIcon = new ImageIcon(getClass().getResource("image/search.gif"));

        listOfUsers = new DefaultTableModel(20, 2);
        listOfUsers.setColumnIdentifiers(columnName);

        UserType = new JLabel("UserType:");
        UserType.setBounds(20, 20, 200, 20 );
        UserType.setFont(new Font("Hobo Std", Font.BOLD, 15));

        Usertype = new JComboBox(listUser);
        Usertype.setBounds(150, 20, 200, 20);

        Username = new JLabel("Username:");
        Username.setBounds(20, 50, 200, 20 );
        Username.setFont(new Font("Hobo Std", Font.BOLD, 15));

        Uname = new JTextField("",20);
        Uname.setBounds(150, 50, 180, 25 );
        Uname.setFont(new Font("Hobo Std", Font.BOLD, 15));

        delete=new JButton("Delete");
        delete.setBounds(340, 300, 85, 25 );
        delete.setFont(new Font("Hobo Std", Font.BOLD, 15));
        delete.addActionListener(this);

        edit=new JButton("Edit");
        edit.setBounds(230, 300, 85, 25 );
        edit.setFont(new Font("Hobo Std", Font.BOLD, 15));
        edit.addActionListener(this);

        search=new JButton("", searchIcon);
        search.setBounds(350, 50, 27, 27);
        search.addActionListener(this);
        search.setFont(new Font("Hobo Std", Font.BOLD, 15));


       UserTable = new JTable(listOfUsers){
                    public boolean isCellEditable(int UsertRow, int UserCol){
                        return false;
                        }};
       UserTable.setPreferredScrollableViewportSize(new Dimension(100, 50));


        //productTable.setFont(new Font("Arial", Font.BOLD, 20));

                                UserTable.setBounds(20, 130, 400, 152);
                                scrollPanel = new JScrollPane(UserTable);
                                scrollPanel.setBounds(23, 130, 400, 152);
                                UserTable.setFillsViewportHeight(true);

        panel=new JPanel(null);
        panel.setBounds(300, 0, 160, 424);
        panel.setBackground(Color.LIGHT_GRAY);
        panel.add(search);
        panel.add(UserType);
        panel.add(Usertype);
        panel.add(delete);
        panel.add(edit);
        panel.add(Uname);
        panel.add(Username);

        UsrModel rm = new UsrModel();
            ArrayList al= new ArrayList();
            ArrayList al1= new ArrayList();
            al= rm.getUser2();
            al1 = rm.getUser3();
            int i=0 ;
            this.size = al.size();
            while( i < al.size() ){
                listOfUsers.setValueAt(al.get(i).toString(), i, 0);
                listOfUsers.setValueAt(al1.get(i).toString(), i, 1);
                i++;
            }

    }

    public void actionPerformed(ActionEvent e){

       if(e.getSource()== delete){
        String n =Uname.getText();
            String ut = Usertype.getSelectedItem().toString();
            String user = null;
            String name = null;
            ResultSet rs;
            UserController uc = new UserController();
            rs = uc.getuser(ut, n);
         try {
                if (rs.next()) {
            try {
                    user = rs.getString("userType");

                } catch (SQLException ex) {
                    Logger.getLogger(Userview.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    name = rs.getString("username");
                } catch (SQLException ex) {
                    Logger.getLogger(Userview.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            } catch (SQLException ex) {
                Logger.getLogger(EditUser.class.getName()).log(Level.SEVERE, null, ex);
            }
            if((n == null ? name != null : !n.equals(name)) && (ut == null ? user != null : !ut.equals(user))){
                JOptionPane.showMessageDialog(null, "Username does not exist!");
            }
           else if(n.isEmpty())
            JOptionPane.showMessageDialog(null, "Username is empty");

        else if((n == null ? name == null : n.equals(name))&&(ut == null ? user == null : ut.equals(user))){
            int i = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete this user?", "Confirm Delete", JOptionPane.YES_NO_OPTION);
            if(i==0){
                UserController us = new UserController();
                int p=us.delteUser(n);
                if(p==0)
                JOptionPane.showMessageDialog(null, "Delete is Successful!");

            }
        
        }
        else
            JOptionPane.showMessageDialog(null, "Username does not exist!");


        }

        else if(e.getSource() == edit){

            String n =Uname.getText();

        if(n.isEmpty())
            JOptionPane.showMessageDialog(null, "Username is empty");

        else if(n.isEmpty()==false){
            this.u=1;
            String ut = Usertype.getSelectedItem().toString();
            String user = null;
            String name = null;
            ResultSet rs;
            UserController uc = new UserController();
            rs = uc.getuser(ut, n);
 try {
                if (rs.next()) {
            try {
                    user = rs.getString("userType");
                   
                } catch (SQLException ex) {
                    Logger.getLogger(Userview.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    name = rs.getString("username");
                } catch (SQLException ex) {
                    Logger.getLogger(Userview.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            } catch (SQLException ex) {
                Logger.getLogger(EditUser.class.getName()).log(Level.SEVERE, null, ex);
            }

            if( (ut == null ? user == null : ut.equals(user)) && (n == null ? name == null : n.equals(name))){


            panel2.removeAll();
            panel2.repaint();

            EditUser ed = new EditUser();
                try {
                    ed.EditUser(ut, n);
                } catch (SQLException ex) {
                    Logger.getLogger(Userview.class.getName()).log(Level.SEVERE, null, ex);
                }
            

            
            panel2.add(ed.getints());
            panel2.add(ed.getUserType());
            panel2.add(ed.getUserName());
            panel2.add(ed.getPassword());
            panel2.add(ed.getConfirmPassword());
            panel2.add(ed.getSecurityQ());
            panel2.add(ed.getSecurityA());
            panel2.add(ed.getuserType());
            panel2.add(ed.getUname());
            panel2.add(ed.getPword());
            panel2.add(ed.getCPword());
            panel2.add(ed.getSQ());
            panel2.add(ed.getSA());
            panel2.add(ed.getsubmit());
            panel2.repaint();
            panel2.revalidate();
      
            }
            else
            JOptionPane.showMessageDialog(null, "Username does not exist!");
            
            
        }
        else
            JOptionPane.showMessageDialog(null, "Username does not exist!");


        }
        else if( e.getSource() == search){
          
            

            UsrModel rm = new UsrModel();
            ArrayList al= new ArrayList();
            ArrayList al1= new ArrayList();
            al= rm.getUser(Usertype.getSelectedItem().toString());
            al1 = rm.getUser1(Usertype.getSelectedItem().toString());
            int i=0 ;
              while( i < this.size ){
                listOfUsers.setValueAt(" ", i, 0);
                listOfUsers.setValueAt(" ", i, 1);
                i++;
            }
            i=0;
            while( i < al.size() ){
                listOfUsers.setValueAt(al1.get(i).toString(), i, 0);
                listOfUsers.setValueAt(al.get(i).toString(), i, 1);
                i++;
            }
        }
    }

    public JLabel getUserType(){
        return UserType;
    }

    public JComboBox getUsertype(){
        return Usertype;
    }

    public JLabel getUsername(){
        return Username;
    }

    public JTextField getUname(){
        return Uname;
    }

    public JTable getuserTable(){
        return UserTable;
    }

    public JScrollPane getscrollPanel(){
        return scrollPanel;
    }

    public JButton getsubmit(){
        return delete;
    }
     public JButton getedit(){
        return edit;
    }

    public int setchoice(){
        return this.u;
    }

    public void getJPanel(JPanel i){
     this.panel2=i;
    }
     public JButton getsearch(){
        return search;
    }
}


